{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is a simple example that shows how to use GapStatClustering to cluster a\n",
    "dataset using the gap statistic to estimate the optimal number of clusters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gapstat import GapStatClustering\n",
    "from sklearn.cluster import AgglomerativeClustering\n",
    "from sklearn.datasets import make_blobs\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# create data set with 5 clusters\n",
    "n_samples = [600, 400, 500, 400, 550]\n",
    "centers = [[4,4],[-4,4],[0,0],[-3.25,-3.25],[4,-4]]\n",
    "cluster_std = [0.5,0.75,1.9,0.75,1.15]\n",
    "\n",
    "X,_ = make_blobs(n_samples=n_samples, centers=centers, cluster_std=cluster_std, n_features=2, random_state=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cluster data set using gap statistic to estimate optimal number of clusters\n",
    "gstat_ac = GapStatClustering(base_clusterer=AgglomerativeClustering(), max_k=7).fit(X)\n",
    "gstat_ac.n_clusters_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# plot the result\n",
    "plt.scatter(X[:,0],X[:,1],c=gstat_ac.labels_)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "gapstat",
   "language": "python",
   "name": "gapstat"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
